package com.zhuoyue.web.controller.aivoice;

import com.alibaba.fastjson.JSON;

import com.zhuoyue.biz.core.api.appjson.dueros.Appliance;
import com.zhuoyue.biz.core.api.appjson.dueros.Header;
import com.zhuoyue.biz.core.api.appjson.dueros.Payload;
import com.zhuoyue.biz.core.api.appjson.dueros.Root;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @author wjming
 */
@Component
public class DuerosStatusReport {
    RestTemplate restTemplate = new RestTemplate();
    private Logger log = Logger.getLogger(this.getClass());


    public void duerosStatus(String deviceId) {
        try {
            Root param = param(deviceId);
            Object postForObject = restTemplate.postForObject("https://xiaodu.baidu.com/saiya/smarthome/changereport", JSON.toJSON(param), String.class);
            System.out.println("postForObject = " + postForObject.toString());
            System.out.println("通知小度");
            log.info("回应给小度的状态信息====>" + postForObject.toString());
        } catch (Exception e) {
            log.info("回应给小度的状态信息====>" + e);
        }

    }

    private static Root param(String deviceId) {
        Appliance appliance = new Appliance();
        Header header = new Header();
        Payload payload = new Payload();
        Root root = new Root();
        header.setNamespace("DuerOS.ConnectedHome.Control");
        header.setName("ChangeReportRequest");
        header.setMessageId("01ebf625-0b89-4c4d-b3aa-32340e894688");
        header.setPayloadVersion("1");

        payload.setBotId("6ec5c00e-dd04-ca76-65f1-930a12f1d97d");
        payload.setOpenUid("e5d50bbca8dbb15ac22463577c14e92f");
        appliance.setApplianceId(deviceId);
        appliance.setAttributeName("powerState");
        payload.setAppliance(appliance);
        root.setHeader(header);
        root.setPayload(payload);
        System.out.println(JSON.toJSON(root));
        return root;
    }


}
